package pub.developers.forum.infrastructure;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import pub.developers.forum.common.model.PageRequest;
import pub.developers.forum.common.model.PageResult;
import pub.developers.forum.common.support.DateUtil;
import pub.developers.forum.domain.entity.Notice;
import pub.developers.forum.domain.entity.User;
import pub.developers.forum.domain.repository.NoticeRepository;
import pub.developers.forum.domain.repository.UserRepository;
import pub.developers.forum.infrastructure.dal.dao.NoticeDAO;
import pub.developers.forum.infrastructure.dal.dataobject.NoticeDO;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class NoticeRepositoryImpl implements NoticeRepository {
    @Resource
    private NoticeDAO noticeDAO;
    @Resource
    private UserRepository userRepository;
    @Override
    public boolean saveNotice(Notice notice) {
        try {
            NoticeDO noticeDO = new NoticeDO();
            BeanUtils.copyProperties(notice, noticeDO);
            noticeDO.setCreateAt(new Date());
            noticeDO.setUpdateAt(new Date());
            int n = noticeDAO.insert(noticeDO);
            if (n > 0){
                return true;
            }
            log.error("保存通知失败 notice:{}", JSONObject.toJSONString(notice));
        }catch (Exception e){
            log.error("保存通知异常 notice:{}", JSONObject.toJSONString(notice), e);
        }
        return false;
    }

    @Override
    public PageResult<Notice> page(PageRequest<Notice> pageRequest) {
        PageHelper.startPage(pageRequest.getPageNo(), pageRequest.getPageSize());
        List<NoticeDO> noticeDOList = noticeDAO.query();
        PageInfo<NoticeDO> pageInfo = new PageInfo<>(noticeDOList);
        if (ObjectUtils.isEmpty(noticeDOList)) {
            return PageResult.build(pageInfo.getTotal(), pageInfo.getSize(), new ArrayList<>());
        }
        List<Notice> notices = new ArrayList<>();
        for (NoticeDO noticeDO : noticeDOList){
            Notice notice = new Notice();
            BeanUtils.copyProperties(noticeDO, notice);
            notice.setCreateTime(DateUtil.toyyyyMMddHHmmss(noticeDO.getCreateAt()));
            notice.setUpdateTime(DateUtil.toyyyyMMddHHmmss(noticeDO.getUpdateAt()));
            if (noticeDO.getPublisher() != null){
                User user = userRepository.get(noticeDO.getPublisher());
                if (!ObjectUtils.isEmpty(user)){
                    notice.setPublisherName(user.getNickname());
                }
            }
            notices.add(notice);
        }
        return PageResult.build(pageInfo.getTotal(), pageInfo.getSize(), notices);
    }

    @Override
    public boolean deleteNotice(Long id, Long userId) {
        int n = noticeDAO.deleteNotice(id , userId);
        return n > 0;
    }

    @Override
    public boolean updateNotice(Notice notice) {
        NoticeDO noticeDO = new NoticeDO();
        BeanUtils.copyProperties(notice, noticeDO);
        noticeDO.setCreateAt(new Date());
        noticeDO.setUpdateAt(new Date());
        int n = noticeDAO.updateNotice(noticeDO);
        return n > 0;
    }
}
